package cn.s1995.sloth.bms.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.ProdCategory;
import cn.s1995.sloth.bizs.service.ProdCategoryService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import cn.s1995.sloth.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分类
 *
 * @author Lex
 * @date 2023-12-14 09:47:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prodCategory")
@Tag(description = "ProdCategory", name = "BMS-产品分类管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProdCategoryController {

    private final ProdCategoryService prodCategoryService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 产品分类
     * @return
     */
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject ProdCategory req) {
        LambdaQueryWrapper<ProdCategory> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(req.getName()), ProdCategory::getName, req.getName());
        return R.ok(prodCategoryService.page(page, wrapper));
    }

    /**
     * 获取列表
     * @param req 产品分类
     * @return
     */
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_view')")
    public R getList(@ParameterObject ProdCategory req) {
        LambdaQueryWrapper<ProdCategory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(req.getName()), ProdCategory::getName, req.getName());
        return R.ok(prodCategoryService.list(wrapper));
    }

    /**
     * 通过id查询产品分类
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(prodCategoryService.getById(id));
    }

    /**
     * 新增产品分类
     * @param req 产品分类
     * @return R
     */
    @Operation(summary = "新增产品分类", description = "新增产品分类")
    @SysLog("新增产品分类")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_add')")
    public R save(@RequestBody ProdCategory req) {
        return R.ok(prodCategoryService.saveCategory(req));
    }

    /**
     * 修改产品分类
     * @param req 产品分类
     * @return R
     */
    @Operation(summary = "修改产品分类", description = "修改产品分类")
    @SysLog("修改产品分类")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_edit')")
    public R updateById(@RequestBody ProdCategory req) {
        return R.ok(prodCategoryService.updateCategory(req));
    }

    /**
     * 通过id删除产品分类
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除产品分类", description = "通过id删除产品分类")
    @SysLog("通过id删除产品分类")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_del')")
    public R removeById(@RequestBody Long[] ids) {
        return prodCategoryService.removeBatchCategory(ids);
        // return R.ok(prodCategoryService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_ProdCategory_export')")
    public List<ProdCategory> export(ProdCategory req, Long[] ids) {
        return prodCategoryService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), ProdCategory::getId, ids));
    }

    @ApiOperationSupport(order = 50)
    @Operation(summary = "获取树", description = "获取分类树")
    @GetMapping(value = "/tree")
    @Inner(value = false)
    public R<List<Tree<Long>>> getTree(Long pid, String name) {
        return R.ok(prodCategoryService.treeMenu(pid, name));
    }

}
